# -*- coding: utf-8 -*-

from django.contrib import admin
from models import Contact, Category, ContactKeeper, ContactManager


class ContactMangerInline(admin.TabularInline):
    model = ContactManager
    extra = 1


class ContactKeeperInline(admin.TabularInline):
    model = ContactKeeper
    extra = 1

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'description', 'picture', 'create_at')
    raw_id_fields = ('user', )
    search_fields = ('name', )


@admin.register(ContactKeeper)
class ContactKeeperAdmin(admin.ModelAdmin):
    list_display = ('id', 'contact', 'owner', 'create_at')
    raw_id_fields = ('contact', 'owner')
    search_fields = ('owner__account__phone', )

@admin.register(ContactManager)
class ContactManagerAdmin(admin.ModelAdmin):
    list_display = ('id', 'contact', 'phone', 'status', 'create_at')
    raw_id_fields = ('contact', 'category', 'user')
    search_fields = ('user__account__phone', )

    def phone(self, obj):
        return '%s' % obj.user.account.phone
    phone.short_description = u'手机号'


@admin.register(Contact)
class ContactAdmin(admin.ModelAdmin):
    inlines = (ContactKeeperInline, ContactMangerInline)
    list_display = ('id', 'name', 'phone', 'email',
                    'country', 'province', 'city', 'district', 'street', 'postcode',  'create_at')
    search_fields = ['name', 'phone']
    list_filter = ('country', 'province', 'city')

    fieldsets = (
        (None, {
            'fields': ('name', 'country', 'province', 'city', 'district', 'street')
        }),
        (u'可选', {
            'classes': ('collapse', ),
            'fields': ('phone', 'email')
        })
    )